<?php
//Making Sure The Image Size Isn't Bigger Than X and Y and resize it if needed
function image_resize($image,$dwidth=100,$dheight=100)
{
	//get image size
	list($nwidth,$nheight)=list($width,$height)=getimagesize($image);
	
	//save unnecessary work
	if ($nwidth<=$dwidth&&$nheight<=$dheight)
	{
	return true;
	}
	
	//calculate new width and height if needed
	if ($nwidth>$dwidth)
	{
	$nwidth=$dwidth;
	$nheight=(int) ((($nwidth/$width)*$nheight)+0.5);
	}
	if($nheight>$dheight)
	{
	$nheight=$dheight;
	$nwidth=(int) ((($nheight/$height)*$nwidth)+0.5);
	}
	
	//get file extention aka file type and create the related resource
	$type=@strtolower(array_pop(explode(".",$image)));
	$imagex=$image;
	if ($type=="jpg"||$type=="jpeg")
	{
	$image=imagecreatefromjpeg($image);
	}
	elseif($type=="gif")
	{
	$image=imagecreatefromgif($image);
	}
	elseif($type=="png")
	{
	$image=imagecreatefrompng($image);
	}
	
	//create a new resource for the new resized image
	$image_p=imagecreatetruecolor($nwidth,$nheight);
	if (!($image&&$image_p))
	{
	return false;
	}

	//Resize the image and output it to a new file
	if (imagecopyresampled($image_p, $image, 0, 0, 0, 0, $nwidth, $nheight, $width, $height))
	{
		if ($type=="jpg"||$type=="jpeg")
		{
		if (!imagejpeg($image_p,$imagex."_temp",90))
		{
		return false;
		}
		
		}
		elseif($type=="gif")
		{
			if (!imagegif($image_p,$imagex."_temp"))
			{
			return false;
			}
		}	
		elseif($type=="png")
		{
			if (!imagepng($image_p,$imagex."_temp",1))
			{
			return false;
			}
		}
		else
		{
		return false;
		}

	}
	else
	{
	return false;
	}
	
	//If everything is fine overwrite the old image :)
	if (rename($imagex."_temp",$imagex))
	{
	return true;
	}
	else
	{
	return false;
	}

}

if (image_resize("./test.jpg"))
{
print "Ok Boss";
}

?>